package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Classlib;
import com.gxa.zhonglong.service.ClasslibService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/25 13:27
 * Description:类库管理操作接口
 * Version: V1.0
 */
@Api(tags ="总部类库接口api")
@CrossOrigin
@RestController
@RequestMapping("/classlib")
public class ClasslibController {

    @Autowired
    private ClasslibService classlibService;

    @PostMapping("/selectByHeadquartersId")
    @ApiOperation("根据总部的id查询所有类库信息")
    @ApiImplicitParam(name = "id",value = "总部id",required = true)
    public Map<String,Object> selectByHeadquartersId(Long id) throws BusinessException {
        final List<Map<String, Object>> maps = classlibService.selectByHeadquartersId(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改类库")
    public Map<String,Object> add(Classlib classlib) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if(classlib.getId()==null){
            classlibService.add(classlib);
            jsonResult.setMsg("添加成功!");
        }else{
            classlibService.update(classlib);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/del")
    @ApiOperation("添加根据id删除类库")
    public Map<String,Object> del(Long id) throws BusinessException {
        classlibService.del(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }
}
